package com.hxl.alert.mq;

import com.hxl.alert.entity.AlertOriginBase;
import com.hxl.alert.utils.DateUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

@Component
public class MessageProducer {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    // 发送消息（同步方式）
    public void send(String topic, String id,String faultDescr) {
        AlertOriginBase message = new AlertOriginBase();
        message.setFaultId(String.valueOf(id));
        message.setDatasourceId(String.valueOf(id));
        message.setFaultDescr(faultDescr);
        message.setSourceApp("1002");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        Calendar nowTime2 = Calendar.getInstance();
        Random random = new Random();
        int nextInt = random.nextInt(5);
        nowTime2.add(Calendar.MINUTE, -(nextInt));
        message.setOccurTime(sdf.format(nowTime2.getTime()));
        rocketMQTemplate.syncSend(topic, message);
        System.out.println("[生产者] 消息已发送: " + message);
    }
}